<title>优惠券明细</title>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="market-coupons_detail-index-search_from">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">卡号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="code" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="market-coupons_detail-index-search_button">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <script type="text/html" id="market-coupons_detail-index-toolbar_tmp">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="market-coupons_detail-index-status_on">禁用</button>
                <button class="layui-btn layui-btn-sm" lay-event="market-coupons_detail-index-status_off">起用</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="market-coupons_detail-index-del">删除</button>
            </div>
        </script>
        <div class="layui-card-body">
            <table id="market-coupons_detail-index" lay-filter="market-coupons_detail-index"></table>
            <script type="text/html" id="market-coupons_detail-index-username_tmp">
                {{# if(d.username){ }}
                {{ d.username }}
                {{# }else{ }}
                <span class="layui-badge">未绑定</span>
                {{# } }}
            </script>
            <script type="text/html" id="market-coupons_detail-index-status_tmp">
                <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定" lay-filter="market-coupons_detail-index-status_btn" {{#
                       if(d.status== 1){ }}checked{{# } }}>
            </script>
            <script type="text/html" id="market-coupons_detail-index-action_button">
                <a class="layui-btn layui-btn-xs" lay-event="market-coupons_detail-index-bind">绑定用户</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="market-coupons_detail-index-del">删除</a>
            </script>
        </div>
    </div>
</div>
<script type="text/html" template lay-done="layui.data.sendParams(d.params)"></script>
<script>
    layui.data.sendParams = function (params) {
        layui.use(['common', 'table'], function () {
            var $ = layui.jquery,
                common = layui.common,
                view = layui.view,
                table = layui.table,
                form = layui.form,
                lay_id_prefix = 'market-coupons_detail-index',//当前页id前缀
                edit_id_prefix = 'market-coupons_detail-add',//添加编辑页id前缀
                edit_view_url = 'market/coupons_detail/add',//添加编辑页模板地址
                api_action_url = layui.setter.apiHost + '/market/coupons_detail';

            //表单搜索
            form.on('submit(' + lay_id_prefix + '-search_button)', function (data) {
                var field = data.field;
                field.page = 1;//默认第一页
                table.reload(lay_id_prefix, {
                    where: field
                });
            });

            //表格操作
            table.render({
                elem: '#' + lay_id_prefix,
                url: api_action_url,
                where: {coupons_id: params.coupons_id},
                toolbar: '#' + lay_id_prefix + '-toolbar_tmp',
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'id', title: 'ID', width: 80},
                    {field: 'code', title: '券号', minWidth: 150},
                    {field: 'username', title: '绑定用户', width: 150, toolbar: '#' + lay_id_prefix + '-username_tmp'},
                    {field: 'is_use', title: '使用状态', width: 100},
                    {field: 'status', title: '状态', width: 100},
                    {field: 'use_at', title: '使用时间', width: 170},
                    {field: 'bind_at', title: '绑定时间', width: 170},
                    {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#' + lay_id_prefix + '-action_button'}
                ]],
                page: true
            });

            //头部工具栏操作
            table.on('toolbar(' + lay_id_prefix + ')', function (obj) {
                var type = obj.event;
                switch (type) {
                    case lay_id_prefix + '-status_on':
                        common.ajax(api_action_url + '/status', lay_id_prefix, {status: 1});
                        break;
                    case lay_id_prefix + '-status_off':
                        common.ajax(api_action_url + '/status', lay_id_prefix, {status: 0});
                        break;
                    case lay_id_prefix + '-del':
                        layer.confirm('确定删除吗', function (index) {
                            common.ajax(api_action_url + '/delete', lay_id_prefix);
                        });
                        break;
                }
            });

            //监听工具条操作按钮
            table.on('tool(' + lay_id_prefix + ')', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case lay_id_prefix + '-del':
                        layer.confirm('确定删除吗', function (index) {
                            if (common.ajax(api_action_url + '/delete', lay_id_prefix, {id: data.id}, false)) {
                                obj.del();
                                layer.close(index);
                            }
                        });
                        break;
                    case lay_id_prefix + '-bind':
                        layer.prompt({title: '请输入用户名', formType: 3}, function (username, index) {
                            form_data = {id: data.id, username: username};
                            common.ajax(api_action_url + '/bind', lay_id_prefix, form_data);
                            layer.close(index);
                        });
                        return false;
                        break;
                }
            });

        });
    }
</script>
